package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.impl.UserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.PhoneVerifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        userRepository.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userRepository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return userRepository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {

        UserInfo userInfo = userRepository.findByPhone(phone);

        return userInfo != null;
    }

    @Override
    public UserInfo checkUsername(String name) {

        return userRepository.findByNickname(name);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        //判断传入是否为空
        AssertUtil.hasLength(phone,"手机号不能为空" );
        AssertUtil.hasLength(nickname,"用户名不能为空" );
        AssertUtil.hasLength(password,"密码不能为空" );
        AssertUtil.hasLength(rpassword,"确认密码不能为空" );
        AssertUtil.hasLength(verifyCode,"验证码不能为空" );

        //判断2次输入密码是否相等
        AssertUtil.isEquals(password, rpassword, "两次输入密码不一致");
        //手机号码格式必须正确

        //手机号必须进行唯一校验
        if (this.checkPhone(phone)){
            throw new LogicException("手机号码已存在");
        }
        //手机短信验证码必须一致
        String code = userInfoRedisService.getVerifyCode(phone);

        if (code == null || !code.equals(verifyCode)){
            throw  new LogicException("验证码已失效或错误");
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);
        userInfo.setEmail("");
        userInfo.setCity("");
        userInfo.setHeadImgUrl("images/default.jpg");
        userInfo.setInfo("");
        userInfo.setLevel(1);
        userInfo.setState(UserInfo.STATE_NORMAL);

        userRepository.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {


        AssertUtil.hasLength(username, "用户名不能为空");
        AssertUtil.hasLength(password, "密码不能为空");

        UserInfo user = userRepository.findByPhone(username);

        if (user == null || !user.getPassword().equals(password)){

            throw new LogicException("账号或者密码错误");

        }

        return user;
    }
}
